package com.link.play.fit.ai.ui.adjust.adjustPage.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.SeekBar
import androidx.lifecycle.ViewModelProvider
import com.link.play.fit.ai.base.BaseFragment
import com.link.play.fit.ai.bean.UserGuide
import com.link.play.fit.ai.databinding.AdjustPageFragment5Binding
import com.link.play.fit.ai.ui.adjust.adjustPage.AdjustPageModel

import kotlin.math.round
import kotlin.math.roundToInt

/**
 *Create by SleepDog on 2025-03-24
 */
class AdjustPageFragment5 :
    BaseFragment<AdjustPageFragment5Binding>(AdjustPageFragment5Binding::inflate) {
    private val viewModel: AdjustPageModel by lazy {
        ViewModelProvider(requireActivity())[AdjustPageModel::class.java]
    }
    private var userGuide: UserGuide? = null
    private var isImperialMetric = false

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initData()
        initListener()
    }

    @SuppressLint("SetTextI18n")
    private fun initData() {
        userGuide = viewModel.getGoalData()
        userGuide?.let {
            isImperialMetric = it.imperialMetric
            var fast = 0f
            if(isImperialMetric){
                binding.valueUnitText.text = "KG"
                binding.seekbarText1.text = "0.1 kg"
                binding.seekbarText2.text = "0.8 kg"
                binding.seekbarText3.text = "1.5 kg"
                fast = it.perWeightKg
                binding.seekBar.max = 15
            }else{
                binding.valueUnitText.text = "lbs"
                binding.seekbarText1.text = "0.2 lbs"
                binding.seekbarText2.text = "1.5 lbs"
                binding.seekbarText3.text = "3.0 lbs"

                fast = it.perWeightLbs
                binding.seekBar.max = 30

            }
            binding.seekBar.progress = (fast * 10).roundToInt()
            binding.valueText.text = "$fast"

        }

        binding.seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                seekBar.progress = progress
                if(progress<=0)return
                val value = (progress.toFloat() / 10)
                binding.valueText.text = "%.1f".format(value)
                val fast = round(value * 10) / 10
                viewModel.setGoalsValue(perWeightKg = fast)

            }

            override fun onStartTrackingTouch(p0: SeekBar?) {
            }

            override fun onStopTrackingTouch(p0: SeekBar?) {
            }

        })
    }

    private fun initListener() {

    }
}